package com.erp.system.controller;

import com.erp.common.controller.BaseController;
import com.erp.common.utils.PageUtils;
import com.erp.common.utils.Query;
import com.erp.common.utils.R;
import com.erp.common.utils.ShiroUtils;
import com.erp.oa.domain.NotifyDO;
import com.erp.oa.service.NotifyService;
import com.erp.system.domain.LeaveDO;
import com.erp.system.domain.UserDO;
import com.erp.system.service.LeaveService;
import com.erp.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Auther: Eric
 * @Date: 2019/9/24 14:31
 * @Description:请假审核
 */
@Controller
@RequestMapping("/system/checkleave")
public class LeaveCheckController extends BaseController {

    @Autowired
    private LeaveService leaveService;
    @Autowired
    private UserService userService;
    @Autowired
    private NotifyService notifyService;


    @GetMapping()
    String Leave(){
        return "system/leave/checkleave";
    }

    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params){
        //查询列表数据
        params.put("status",2);
        Query query = new Query(params);
        List<LeaveDO> leaveList = leaveService.list(query);
        int total = leaveService.count(query);
        PageUtils pageUtils = new PageUtils(leaveList, total);
        return pageUtils;
    }


    /**
     * 审核通过
     */
    @PostMapping( "/check")
    @ResponseBody
    public R check(Long id){
        LeaveDO leave = new LeaveDO();
        leave.setStatus(3);
        leave.setId(id);
        if(leaveService.update(leave)>0){
            setNoify(id, ",申请通过");
            return R.ok();
        }
        return R.error();
    }

    /**
     * 审核拒绝
     */
    @PostMapping( "/checkno")
    @ResponseBody
    public R checkno(Long id){
        LeaveDO leave = new LeaveDO();
        leave.setStatus(5);
        leave.setId(id);
        if(leaveService.update(leave)>0){
            setNoify(id, ",申请被拒绝");
            return R.ok();
        }
        return R.error();
    }

    private void setNoify(Long id, String message) {
        LeaveDO leaveDO = leaveService.get(id);
        //发通知和邮件通知采购和总经理
        NotifyDO notify = new NotifyDO();
        notify.setType("6");
        notify.setTitle("请假申请");
        List<Long> deprids = new ArrayList<>();
        deprids.add(16L);
        List<UserDO> userDOList = userService.getUsersByDeptIds(deprids);
        List<Long> userId = userDOList.stream().map(UserDO::getUserId).collect(Collectors.toList());
        notify.setContent(leaveDO.getUserNameCreate() + "申请请假 /n" +
                "请假事由:" + leaveDO.getReason() + message);
        userId.add(leaveDO.getDeptManagerid());
        Long[] userIds = userIds = userId.toArray(new Long[userId.size()]);
        notify.setUserIds(userIds);
        notify.setStatus("1");
        notify.setCreateBy(getUserId());
        notifyService.save(notify);
    }
}
